package com.changgou.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.util.CanalUtil;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.UpdateListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@CanalEventListener
public class SpuListener {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @UpdateListenPoint(schema = "changgou_goods", table = {"tb_spu"})
    public void spuUpdate(CanalEntry.RowData rowData){
        //获取修改前数据
        Map<String, String> beforeMap = CanalUtil.convertToMap( rowData.getBeforeColumnsList() );
        //修改后数据
        Map<String, String> afterMap = CanalUtil.convertToMap( rowData.getAfterColumnsList() );

        //如果上架状态修改前的0，修改后的是1，则属于上架操作
        if( "0".equals(beforeMap.get( "is_marketable" ))  && "1".equals( afterMap.get( "is_marketable" ) )  ){
            System.out.println("监控到tb_spu表变化（上架操作），发送到mq");
            rabbitTemplate.convertAndSend( "goods_up_exchange","", afterMap.get( "id" ) );
        }

        //如果下架状态修改前的1，修改后的是0，则属于上架操作
        if( "1".equals(beforeMap.get( "is_marketable" ))  && "0".equals( afterMap.get( "is_marketable" ) )  ){
            System.out.println("监控到tb_spu表变化（下架操作），发送到mq");
            rabbitTemplate.convertAndSend( "goods_down_exchange","", afterMap.get( "id" ) );
        }

    }

}
